﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace NotificationLib
{
    public class EmailNotification : Notification
    {

        public string MailServer { get; set; }
        public MailAddress To { get; set; }
        

        protected  EmailNotificationConfig Config { get; set; }
        

        public EmailNotification(EmailNotificationConfig config, string title, string message, string to )
        {
            Config = config;
            To = new MailAddress(to);

            Title = title;
            Message = message;
     

            
        }

        public override bool Send()
        {

            MailMessage msg = new MailMessage(Config.FromAddress, To);
            msg.Subject = Title;
            msg.Body = Message;
            msg.IsBodyHtml = false;

            switch (Config.Priority)
            {
                case NotificationPriority.Low: { msg.Priority = MailPriority.Low; break; }
                case NotificationPriority.NormalMedium: { msg.Priority = MailPriority.Normal; break; }
                case NotificationPriority.High: { msg.Priority = MailPriority.High; break; }
                case NotificationPriority.Emergency: { msg.Priority = MailPriority.High; break; }
            }


            using (SmtpClient client = new SmtpClient(Config.MailServer))
            {
                try
                {

                    switch (Config.AuthenticationType)
                    {

                        case EmailNotificationAuthenticationType.UserAndPass:
                            {
                                client.Port = Config.MailServerPort;
                                client.EnableSsl = true;
                                client.Credentials = new NetworkCredential(Config.Username, Config.Password);
                            }
                            client.Send(msg);

                            return true;



                        default:
                            {
                                return false;

                            }
                    }


                }
                catch(Exception e)
                {

                    return false;
                }
            }
            
            
        }
    }
}
